package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.InterventionDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.Result;
import com.itheima.pojo.FormDataAdvice;
import com.itheima.pojo.Intervention;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.InterventionService;
import com.itheima.service.OrderService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = InterventionService.class)
@Transactional
public class InterventionServiceImpl implements InterventionService {
    @Autowired
    private InterventionDao interventionDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private SetmealDao setmealDao;

    @Override
    public Result add(FormDataAdvice formDataAdvice, String[] exerciseIds, String[] foodIds) {



       String OrderStatus =interventionDao.findOrderStatus(formDataAdvice.getOrder_id());

       if (OrderStatus.equals("未到诊")){
           return new Result(false,"该体检人未到诊,无法进行健康建议");
       }

        long count=  interventionDao.findId(formDataAdvice.getOrder_id());
      if (count>0){
          return new Result(false,"不允许再次建议,如需再次建议请您点击更新按钮");
      }


        interventionDao.add(formDataAdvice);
        setInterventionAndExercise(formDataAdvice.getId(),exerciseIds);
        setInterventionAndfood(formDataAdvice.getId(),foodIds);
        return new Result(true,"添加成功");
    }


    private void setInterventionAndExercise(Integer formDataAdviceId, String[] exerciseIds) {
        //对参数进行判断
        if (formDataAdviceId != null && exerciseIds.length > 0) {
            for (String exerciseId : exerciseIds) {
                HashMap<String, Integer> map = new HashMap<>();
                int substring = Integer.parseInt(exerciseId.substring(1));
                map.put("formDataAdviceId", formDataAdviceId);
                map.put("exerciseId", substring);
                interventionDao.setInterventionAndExercise(map);
            }
        }
    }

    private void setInterventionAndfood(Integer formDataAdviceId, String[] foodIds) {
        //对参数进行判断
        if (formDataAdviceId != null && foodIds.length > 0) {
            for (String foodId : foodIds) {
                HashMap<String, Integer> map = new HashMap<>();
                int substring = Integer.parseInt(foodId.substring(1));
                map.put("formDataAdviceId", formDataAdviceId);
                map.put("foodId", substring);
                interventionDao.setInterventionAndfood(map);
            }
        }
    }

    @Override
    public FormDataAdvice findById(Integer interventionId) {
        return  interventionDao.findById(interventionId);
    }

    @Override
    public List<Integer> findexerciseIdsByinterventionId(Integer interventionId) {
        return  interventionDao.findexerciseIdsByinterventionId(interventionId);
    }

    @Override
    public List<Integer> findfoodIdsByinterventionId(Integer interventionId) {
        return interventionDao.findfoodIdsByinterventionId(interventionId);
    }

    @Override
    public Result edit(FormDataAdvice formDataAdvice, String[] exerciseIds, String[] foodIds) {
        interventionDao.edit(formDataAdvice);
        interventionDao.deleteFoodId(formDataAdvice.getId());
        interventionDao.deleteExerciseId(formDataAdvice.getId());
        int formDataAdviceId = formDataAdvice.getId();
        /*setInterventionAndExercise(formDataAdvice.getOrder_id(),exerciseIds);*/
            for (String exerciseId : exerciseIds) {
                HashMap<String, Integer> map = new HashMap<>();
                int substring = Integer.parseInt(exerciseId);
                map.put("formDataAdviceId", formDataAdviceId);
                map.put("exerciseId", substring);
                interventionDao.setInterventionAndExercise(map);
            }

        /*setInterventionAndfood(formDataAdvice.getOrder_id(),foodIds);*/
        for (String foodId : foodIds) {
            HashMap<String, Integer> map = new HashMap<>();
            int substring = Integer.parseInt(foodId);
            map.put("formDataAdviceId", formDataAdviceId);
            map.put("foodId", substring);
            interventionDao.setInterventionAndfood(map);
        }
        return new Result(true,"成功");
    }

    @Override
    public Map<String,Object> findHealthInterventionByOrderId(Integer orderId) {
        Map map = new HashMap();
        Order order=orderDao.findOrderById(orderId);
        if (order.getOrderStatus().equals("未到诊")){
            return null;
        }
        if (order!=null) {
            map.put("orderDate", order.getOrderDate());
            map.put("memberName", order.getName());
        }


        Setmeal setmeal = setmealDao.findSetmealByOrderId(orderId);
        if (setmeal!=null) {
            map.put("img", setmeal.getImg());
            map.put("setmealName", setmeal.getName());
        }

        Intervention intervention=interventionDao.findByOrderId(orderId);
        if (intervention!=null){
            map.put("doctorName",intervention.getDoctorName());
            map.put("doctorAdvice",intervention.getDoctorAdvice());
        }
        return map;

    }
}
